<?php
	session_start();

	require_once("includes/connect.php");
	require_once("includes/function.php");

	$panngan2 = $_REQUEST["panngan2"];
	$panngan3 = $_REQUEST["panngan3"];

	$submit = $_REQUEST["submit"];

	$budget_year = $_REQUEST["budget_year"];

	$month_year = $_REQUEST["month_year"];
	
	$ex1_id = $_REQUEST["ex1_id"];
	$ex2_id = $_REQUEST["ex2_id"];
	$ex3_id = $_REQUEST["ex3_id"];
?>

<script type="text/javascript">
	$(document).ready(function(){

		$.post('dropdown.php', {  tablename: 'expenses_lvl1', dd: 'ex1_id', idname: 'expenses1_id', fieldname: 'expenses1_name', by: '04', ex1_id:'<?php echo $ex1_id; ?>', ex2_id:'<?php echo $ex2_id; ?>', ex3_id:'<?php echo $ex3_id; ?>'  }, function(data) { $("#expenses1").html(data); } );

		$("input,select,textarea").not("[type=submit]").jqBootstrapValidation({
			submitSuccess: function ($form, event) { 
				//event.preventDefault();
			}
		});

		$( "#panngan2" ).change(function() {
			var panngan2= $(this).val();
			var panngan3 = "<?php echo $panngan3; ?>";
			
			//alert(panngan2);
			
			if (panngan2 != "") {
				//alert(panngan2);
				$.post('dd_panngan.php', {  panngan2: panngan2, panngan3: panngan3}, function(data) { 
					$("#dd_panngan3").html(data); 

				} );
			}

		});

		$("#month_year").datepicker({

			dateFormat: "yy-mm"
			//defaultDate: new Date()
			//showMonthAfterYear:true,
			
		});
		
		$('#panngan2').trigger('change');

		$("#save_salary").click(function() {
			var val = [];
			var count=0;
			$(':checkbox:checked').each(function(i){
			  val[i] = $(this).val();
			  count++;
			  //alert(val[i]);
			});
			//alert(count);
			if (count == 0) {
				alert ("กรุณาเลือกข้อมูลที่ต้องบันทึกด้วยครับ");
			} else {
				$.post('process_petition_welfare.php', {  panngan2: '<?php echo $panngan2; ?>', panngan3: '<?php echo $panngan3; ?>', month_year: '<?php echo $month_year; ?>', budget_year:'<?php echo $budget_year; ?>',ex1_id:'<?php echo $ex1_id; ?>', ex2_id:'<?php echo $ex2_id; ?>', ex3_id:'<?php echo $ex3_id; ?>', val: val, task:'new'}, function(data) { 
					//$("#test").html(data); 
					
					//if(confirm('คุณต้องการพิมพ์ฎีกาเงินเดือนหรือไม่?')==true){
							//alert('Going to http://www.thaicreate.com');
						//window.location.href = 'prt_welfare?pet_id=' + data;
						//window.open("prt_salary?pet_id=" + data, "_blank");
						//window.location.href = "index.php?pages=frm_petition_salary&pageid=1";
						//alert("print");
					//} else 	{
						window.location.href = "index.php?pages=frm_petition_welfare&pageid=2";
					//}
					

					//alert(data);

				} );
				
			}
		});
		
	}); //(document).ready

	
</script>
<div id="test"></div>
<form id="frm_petition_welfare_add" name="frm_petition_welfare_add" method="post" action="index.php" novalidate="">
	<input type="hidden" name="pages" value="frm_petition_welfare_add">
	<input type="hidden" name="pageid" value="2">
	<div class="row-fluid">
		<div class="control-group span2">
			<label class="control-label" for="budget_year">ปีงบประมาณ</label>
			<div class="controls ">
				<select class="span12" id="budget_year" name="budget_year" readonly>
					<option>2556</option>
					<option>2557</option>
					<option>2558</option>
					<option>2559</option>
					<option>2560</option>
				</select>
			</div>
		</div>

		<div class="control-group span4">
			<label class="control-label" for="panngan">แผนงาน</label>
			<div class="controls ">
				<select class="span12" id="panngan2" name="panngan2" required>
					<option value="">เลือก...</option>
				<?php


					$db = NEW myDBS;
					$db->selectDB("hr_udoncity");
					
					$sql = "SELECT DISTINCT panngan2_id 
								FROM position 
								WHERE lvl_id like '" .$_SESSION["lvl1_id"]. "%'
								AND panngan2_id IS NOT NULL"
								; 

					//echo $sql;

					$result = $db->runSQL($sql);
					
					while($rows = $db->fetch_object ( $result )){
						$db->selectDB("account");
						$sql = "SELECT DISTINCT * FROM panngan2 WHERE panngan2_id = '" .$rows->panngan2_id. "'";
						$result1 = $db->runSQL($sql);
						while($rows1 = $db->fetch_object ( $result1 )){
							if ($rows1->panngan2_id == $panngan2) {
								$selected = " selected";

							} else {
								$selected = "";
							}
							echo "<option value='".$rows1->panngan2_id."' $selected>$rows1->panngan2_name</option>";
						}
					}

				?>
				</select>
			</div>
		</div><!--/span-->

		<div class="control-group span4">
			<label class="control-label" for="panngan3">งาน</label>
			<div class="controls " id="dd_panngan3">
				<select class="span12" id="panngan3" name="panngan3">
					<option value="">เลือก</option>

				</select>
			</div>
		</div><!--/span-->

		<div class="control-group span2">
			<label class="control-label" for="month_year">เดือน/ปี</label>
			<div class="controls">
				<input class="span12" type="text" id="month_year" name="month_year" value="<?php echo $month_year; ?>" required />
            </div>
		</div>
	</div><!--/row-fluid-->

	<div class="row-fluid">
		<div class="control-group span4">
			<label class="control-label" for="expenses1_name">หมวด / ลักษณะ</label>
			<div id="expenses1" class="controls">
				<select class="span12" id="ex1_id" name="ex1_id" required>
					<option value="">เลือก...</option>
				</select>
            </div>
		</div>

		<div class="control-group span3">
			<label class="control-label" for="expenses2_name">ประเภท</label>
			<div id="expenses2" class="controls">
				<select class="span12" id="ex2_id" name="ex2_id" required>
					<option value="">เลือก...</option>
				</select>
            </div>
		</div>

		<div class="control-group span3">
			<label class="control-label" for="expenses3_name">รายการ</label>
			<div id="expenses3" class="controls">
				<select class="span12" id="ex3_id" name="ex3_id" required>
					<option value="">เลือก...</option>
				</select>
            </div>
		</div>

		<div class="control-group span2">
			<label class="control-label" for="blank">&nbsp;</label>
			<button type="submit" id="submit" name="submit"  class="btn btn-info"><i class="icon-ok icon-white"></i>ดึงข้อมูล</button>
			<!--<a href="index.php?pages=home" role="button" class="btn"><i class="icon-remove"></i>ยกเลิก</a>-->
		</div>
	</div><!--/row-fluid-->

</form> 
<?php
	if (isset($submit)) {
		//echo "onsubmit";
		$sql = "SELECT * 
				FROM  tbl_init 
				WHERE  panngan2_id =  '$panngan2'
				AND  panngan3_id =  '$panngan3'
				AND  month_year =  '$month_year'
				AND  budget_year =  '$budget_year'
				";

		switch ($ex3_id) {
			 case 23:
				$sql = $sql . "AND rent_home > 0";
				 break;
			 case 24:
				$sql = $sql . "AND education > 0";
				 break;
			 case 25:
				 $sql = $sql . "AND medical_fee > 0";
				 break;
			}
		//echo $sql;
		$db->selectDB("account");
		$results = $db->runSQL($sql);
?>
	<button id="save_salary" name = "save_salary" class="btn btn-primary">บันทึกฏีกาสวัสดิการ</button>
	<table id="example" class="table table-bordered" width="100%" >
		<caption><br></caption>
		<thead>
			<tr>
				<th>#</th>
				<th>เลขประจำตัว</th>
				<th>ชื่อ - นามสกุล</th>
				<th>ตำแหน่ง</th>
				<th>
				<?php
					switch ($ex3_id) {
					 case 23:
						echo "ค่าเช่าบ้าน";
						 break;
					 case 24:
						echo "เงินช่วยเหลือการศึกษาบุตร";
						 break;
					 case 25:
						echo "เงินช่วยเหลือค่ารักษาพยาบาล";
						 break;
					}
				?>
				</th>
				<th>action</th>
				<th>สถานะ</th>
			</tr>
		</thead>
		<tbody>
<?php
		$i=0;
		$db1 = NEW myDBS;
		$db1->selectDB("hr_udoncity");

		while ($rows = $db->fetch_object($results)) {
			$sql = "SELECT fname, lname, position_name FROM person, position
					WHERE position.id = person.id
					AND person.id = '". $rows->card_id ."'
					";
			$results1 = $db1->runSQL($sql);
			//echo $sql;
			$rows1 = $db1->fetch_object($results1);
			$i++;
			echo "<tr>
						<td>$i</td>
						<td>$rows->card_id</td>
						<td>$rows1->fname&nbsp;&nbsp;&nbsp;$rows1->lname</td>
						<td>$rows1->position_name</td>
						<td align='right'>
						";
				 
						switch ($ex3_id) {
							case 23:
								echo number_format($rows->rent_home, 2);
								break;
							case 24:
								echo number_format($rows->education, 2);
								break;
							case 25:
								echo number_format($rows->medical_fee, 2);
								break;

						}
			echo "</td>";
			echo "<td><input type='checkbox' name='check[]' id='check_$rows->card_id' value='$rows->card_id' /></td>  ";
			echo "<td>";
				switch ($ex3_id) {
					 case 23:
						if (strpos($rows->status, 'rent_home') <> false) {
							echo "เบิกค่าเช่าบ้านแล้ว";
						}
						 break;
					 case 24:
						if (strpos($rows->status, 'education') <> false) {
							echo "เบิกค่าศึกษาบุตรแล้ว";
						}
						 break;
					 case 25:
						if (strpos($rows->status, 'medical_fee') <> false) {
							echo "เบิกค่ารักษาพยาบาลแล้ว";

						}
						 break;
				 }
			echo "</td>";
			echo "</tr>";

		}
?>
	</tbody>
	</table>
<?php
	}
?>
